import xarray as xr
import sys
# 运行目录需要放在根目录
def inspect_nc_file(file_path):
    try:
        ds = xr.open_dataset(file_path)
    except Exception as e:
        print(f"无法打开文件: {file_path}\n错误: {e}")
        sys.exit(1)

    print(f"\n✅ 成功打开文件: {file_path}")
    print("\n📌 文件包含的变量:")
    for var in ds.data_vars:
        var_obj = ds[var]
        print(f" - {var}: shape={var_obj.shape}, dims={var_obj.dims}, dtype={var_obj.dtype}")

    print("\n📌 坐标信息:")
    for coord in ds.coords:
        print(f" - {coord}: values={ds[coord].values[:5]}{'...' if ds[coord].size > 5 else ''}")

    print("\n📌 全部维度信息:")
    for dim in ds.dims:
        print(f" - {dim}: size={ds.dims[dim]}")

    ds.close()

# 使用示例
if __name__ == "__main__":
    # nc_file_path = "panguresults/output_surface_2023-01-16-00-00.nc"  # 改成你的实际路径
    # nc_file_path = "surface_2023-01-16.nc"   # 改成你的实际路径
    nc_file_path = "results/pred_003.nc"  # 改成你的实际路径
    inspect_nc_file(nc_file_path)
